#include "ProgressBar.h"
namespace NBGUI
{
	ProgressBar::ProgressBar(GUIDrawer* drawer,  const char* name, const char* eleText, int eleType, Rect& _rect, float _progress):
		ElementNode(drawer, name,eleText,eleType,_rect),Grid3x3(2,2,64,64),progress(_progress)
	{
 
 
  		uv[0] = vec2f(128.0f/512,0);
  		uv[1] = vec2f(128.0f/512,64.0f/512);
  		uv[2] = vec2f(192.0f/512,64.0f/512);
  		uv[3] = vec2f(192.0f/512,0);
  
  		uv[4] = vec2f(192.0f/512,0);
  		uv[5] = vec2f(192.0f/512,64.0f/512);
  		uv[6] = vec2f(256.0f/512,64.0f/512);
  		uv[7] = vec2f(256.0f/512,0);

		// TODO!!!
		
		

	}

	ProgressBar::~ProgressBar()
	{

	}

	void ProgressBar::buildVI( std::vector<Vertex>* vertices, std::vector<unsigned int>* indices,unsigned int screenWidth, unsigned int screenHeight )
	{
		startIndex = indices->size();
		//background
		backVertexOffset = vertices->size();
		for (int i = 0; i < 9; ++i)
		{
			Rect gridRect = grid[i];
			float left, right, top, bottom;



			if(i == TOPRIGHT || i == MIDRIGHT || i == BOTTOMRIGHT)
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + rect.w - edgeWidth;
			}
			else
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + (float)gridRect.x ;
			}



			if(i == TOPMID || i == MIDMID || i == BOTTOMMID)
			{
				//mid grid , draw as the xml says
				right = left + rect.w - 2 * edgeWidth;
			}
			else
			{
				right = left + (float)gridRect.w ;
			}

			if (i > MIDRIGHT && i <= BOTTOMRIGHT)
			{
				//bottom 3 grid
				top = (float)screenHeight / 2 - (float)rect.y - (rect.h - edgeHeight);
			}
			else
			{
				top = (float)screenHeight / 2 - (float)rect.y -  (float)gridRect.y;
			}

			if(i > TOPRIGHT && i <= MIDRIGHT)
			{
				bottom = top - (rect.h - 2 * edgeHeight) ;
			}
			else
			{
				bottom = top - (float)gridRect.h ;
			}



			Vertex eleVertices[4];
			unsigned int current = vertices->size();
			unsigned int eleIndices[6]={0,1,2,0,2,3};


			eleVertices[0].pos = vec3f(left, top, 0.0f);  // Top left.
			eleVertices[0].coord = uv[0];
			eleVertices[0].coord.x += gridRect.x/512.0f;
			eleVertices[0].coord.y += gridRect.y/512.0f;

			eleVertices[1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
			eleVertices[1].coord = eleVertices[0].coord;
			eleVertices[1].coord.y += gridRect.h/512.0f;

			eleVertices[2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
			eleVertices[2].coord = eleVertices[0].coord;
			eleVertices[2].coord.x += gridRect.w/512.0f;
			eleVertices[2].coord.y += gridRect.h/512.0f;

			eleVertices[3].pos = vec3f(right, top, 0.0f);  // Top right.
			eleVertices[3].coord = eleVertices[0].coord;
			eleVertices[3].coord.x += gridRect.w/512.0f;

			for (int i = 0; i < 4; ++i)
			{
				vertices->push_back(eleVertices[i]);
			}



			for (int i = 0; i  < 6; ++i)
			{
				indices->push_back(current + eleIndices[i]);
			}

			indexLen += 6;
		}
		//////////////////////////////////////////////////////////////////////////
		//front ground (the progress)
		frontVertexOffset = vertices->size();
		for (int i = 0; i < 9; ++i)
		{
			Rect gridRect = grid[i];
			float left, right, top, bottom;



			if(i == TOPRIGHT || i == MIDRIGHT || i == BOTTOMRIGHT)
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + rect.w * progress - edgeWidth;
			}
			else
			{
				left = (-(float)screenWidth / 2) + (float)rect.x + (float)gridRect.x ;
			}



			if(i == TOPMID || i == MIDMID || i == BOTTOMMID)
			{
				//mid grid , draw as the xml says
				right = left + rect.w * progress - 2 * edgeWidth;
			}
			else
			{
				right = left + (float)gridRect.w ;
			}

			if (i > MIDRIGHT && i <= BOTTOMRIGHT)
			{
				//bottom 3 grid
				top = (float)screenHeight / 2 - (float)rect.y - (rect.h - edgeHeight);
			}
			else
			{
				top = (float)screenHeight / 2 - (float)rect.y -  (float)gridRect.y;
			}

			if(i > TOPRIGHT && i <= MIDRIGHT)
			{
				bottom = top - (rect.h - 2 * edgeHeight) ;
			}
			else
			{
				bottom = top - (float)gridRect.h ;
			}



			Vertex eleVertices[4];
			unsigned int currentVertexOffset = vertices->size();
			unsigned int eleIndices[6]={0,1,2,0,2,3};


			eleVertices[0].pos = vec3f(left, top, 0.0f);  // Top left.
			eleVertices[0].coord = uv[4];
			eleVertices[0].coord.x += gridRect.x/512.0f;
			eleVertices[0].coord.y += gridRect.y/512.0f;

			eleVertices[1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
			eleVertices[1].coord = eleVertices[0].coord;
			eleVertices[1].coord.y += gridRect.h/512.0f;

			eleVertices[2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
			eleVertices[2].coord = eleVertices[0].coord;
			eleVertices[2].coord.x += gridRect.w/512.0f;
			eleVertices[2].coord.y += gridRect.h/512.0f;

			eleVertices[3].pos = vec3f(right, top, 0.0f);  // Top right.
			eleVertices[3].coord = eleVertices[0].coord;
			eleVertices[3].coord.x += gridRect.w/512.0f;

			for (int i = 0; i < 4; ++i)
			{
				vertices->push_back(eleVertices[i]);
			}



			for (int i = 0; i  < 6; ++i)
			{
				indices->push_back(currentVertexOffset + eleIndices[i]);
			}

			indexLen += 6;
		}
	}



	void ProgressBar::draw()
	{
		if(hide)
			return;
		pDrawer->pRenderer->applyTexture(0);
		if(progress > 0)
		{
			pDrawer->pRenderer->drawIndexed(startIndex,indexLen,pDrawer->vboId,pDrawer->iboId);
			pDrawer->font->drawStrPack(textDrawingPack);
		}
		else
		{
			pDrawer->pRenderer->drawIndexed(startIndex,indexLen - 54, pDrawer->vboId,pDrawer->iboId);
			pDrawer->font->drawStrPack(textDrawingPack);
		}

		for(auto it = children.begin(); it != children.end(); ++it)
		{
			(*it)->draw();
		}
	}

	void ProgressBar::refreshProgress(float deltaProgress)
	{
		RendererBase* pRenderer = pDrawer->pRenderer;
		progress += deltaProgress;
		if (progress > 1.0f)
		{
			progress = 1.0f;
		}
		if (progress < 0)
		{
			progress = 0;
		}
		
		Vertex eleVertices[36]; 

		for (int i = 0; i < 9; ++i)
		{
			Rect gridRect = grid[i];
			float left, right, top, bottom;



			if(i == TOPRIGHT || i == MIDRIGHT || i == BOTTOMRIGHT)
			{
				left = (-(float)pRenderer->screenWidth / 2) + (float)rect.x + rect.w * progress - edgeWidth;
			}
			else
			{
				left = (-(float)pRenderer->screenWidth / 2) + (float)rect.x + (float)gridRect.x ;
			}



			if(i == TOPMID || i == MIDMID || i == BOTTOMMID)
			{
				//mid grid , draw as the xml says
				right = left + rect.w * progress - 2 * edgeWidth;
			}
			else
			{
				right = left + (float)gridRect.w ;
			}

			if (i > MIDRIGHT && i <= BOTTOMRIGHT)
			{
				//bottom 3 grid
				top = (float)pRenderer->screenHeight / 2 - (float)rect.y - (rect.h - edgeHeight);
			}
			else
			{
				top = (float)pRenderer->screenHeight / 2 - (float)rect.y -  (float)gridRect.y;
			}

			if(i > TOPRIGHT && i <= MIDRIGHT)
			{
				bottom = top - (rect.h - 2 * edgeHeight) ;
			}
			else
			{
				bottom = top - (float)gridRect.h ;
			}



			

			eleVertices[i*4+0].pos = vec3f(left, top, 0.0f);  // Top left.
			eleVertices[i*4+0].coord = uv[4];
			eleVertices[i*4+0].coord.x += gridRect.x/512.0f;
			eleVertices[i*4+0].coord.y += gridRect.y/512.0f;
						 
			eleVertices[i*4+1].pos = vec3f(left, bottom, 0.0f);  // Bottom left.
			eleVertices[i*4+1].coord = eleVertices[i*4+0].coord;
			eleVertices[i*4+1].coord.y += gridRect.h/512.0f;
						 
			eleVertices[i*4+2].pos = vec3f(right, bottom, 0.0f);  // Bottom right.
			eleVertices[i*4+2].coord = eleVertices[i*4+0].coord;
			eleVertices[i*4+2].coord.x += gridRect.w/512.0f;
			eleVertices[i*4+2].coord.y += gridRect.h/512.0f;
						 
			eleVertices[i*4+3].pos = vec3f(right, top, 0.0f);  // Top right.
			eleVertices[i*4+3].coord = eleVertices[i*4+0].coord;
			eleVertices[i*4+3].coord.x += gridRect.w/512.0f;

			 
			
			 
		}
		std::vector<Vertex>* vertices = pDrawer->playout->vertices;
		memcpy_s(&(*vertices)[frontVertexOffset],36*sizeof(Vertex),eleVertices,36*sizeof(Vertex));
		pRenderer->updateVBO(&vertices->front(),0,vertices->size(),sizeof(Vertex), pDrawer->vboId);

	}

	void ProgressBar::handleEvent( Event* e )
	{
		switch(e->type)
		{
		case KEY_PRESS:
			if( static_cast<KeyEvent*>(e)->virtualKey == VK_SPACE )
			{
				refreshProgress(-progress);
			}
			
			break;
		//case MOUSE_BUTTON_RELEASE:
		//	refreshProgress(0.1f);
		//	break;
		}

	}

	void ProgressBar::doSomething(std::vector<info>& i, std::string& funcName)
	{
		if(funcName == std::string("refreshProgress"))
			refreshProgress(i.front().f);
	}



}


